<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>任务管理系统 - 仪表盘</title>
    <!-- 引入Bootstrap CSS -->
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入Font Awesome -->
    <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">
    <!-- 引入Chart.js -->
    <script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
    <!-- 引入自定义样式 -->
    <link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}">
</head>
<body>
    <!-- 导航栏 -->
    <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
        <div class="container-fluid">
            <a class="navbar-brand" href="{{ url_for('dashboard') }}">
                <i class="fa fa-tasks mr-2"></i>任务管理系统
            </a>
            <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="切换导航">
                <span class="navbar-toggler-icon"></span>
            </button>
            <div class="collapse navbar-collapse" id="navbarNav">
                <ul class="navbar-nav me-auto mb-2 mb-lg-0">
                    <li class="nav-item">
                        <a class="nav-link active" aria-current="page" href="{{ url_for('dashboard') }}">
                            <i class="fa fa-dashboard mr-1"></i>仪表盘
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('task_list') }}">
                            <i class="fa fa-list mr-1"></i>任务列表
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('category_list') }}">
                            <i class="fa fa-folder mr-1"></i>分类管理
                        </a>
                    </li>
                    <li class="nav-item">
                        <a class="nav-link" href="{{ url_for('reports') }}">
                            <i class="fa fa-bar-chart mr-1"></i>数据报表
                        </a>
                    </li>
                </ul>
                <div class="navbar-nav">
                    <li class="nav-item dropdown">
                        <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                            <i class="fa fa-user-circle mr-1"></i>{{ user.username }}
                            {% if unread_notifications > 0 %}
                            <span class="badge bg-danger ml-1">{{ unread_notifications }}</span>
                            {% endif %}
                        </a>
                        <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                            <li>
                                <a class="dropdown-item" href="{{ url_for('notifications') }}">
                                    <i class="fa fa-bell mr-1"></i>通知
                                    {% if unread_notifications > 0 %}
                                    <span class="badge bg-danger ml-1">{{ unread_notifications }}</span>
                                    {% endif %}
                                </a>
                            </li>
                            <li>
                                <a class="dropdown-item" href="{{ url_for('settings') }}">
                                    <i class="fa fa-cog mr-1"></i>设置
                                </a>
                            </li>
                            <li><hr class="dropdown-divider"></li>
                            <li>
                                <a class="dropdown-item" href="{{ url_for('logout') }}">
                                    <i class="fa fa-sign-out mr-1"></i>退出登录
                                </a>
                            </li>
                        </ul>
                    </li>
                </div>
            </div>
        </div>
    </nav>

    <!-- 主要内容区域 -->
    <div class="container-fluid mt-4">
        <!-- 消息提示区域 -->
        {% with messages = get_flashed_messages(with_categories=true) %}
            {% if messages %}
                {% for category, message in messages %}
                    <div class="alert alert-{{ category }} alert-dismissible fade show" role="alert">
                        {{ message }}
                        <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="关闭"></button>
                    </div>
                {% endfor %}
            {% endif %}
        {% endwith %}

        <!-- 欢迎信息 -->
        <div class="mb-4">
            <h1 class="display-5">欢迎回来，{{ user.username }}！</h1>
            <p class="text-muted">今天是 {{ datetime.utcnow().strftime('%Y年%m月%d日 %H:%M:%S') }}</p>
        </div>

        <!-- 统计卡片 -->
        <div class="row mb-4">
            <div class="col-md-3 mb-3">
                <div class="card bg-primary text-white h-100">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <p class="card-text text-sm font-semibold">总任务数</p>
                                <h3 class="card-title font-bold">{{ total_tasks }}</h3>
                            </div>
                            <i class="fa fa-tasks fa-3x opacity-50"></i>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card bg-warning text-white h-100">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <p class="card-text text-sm font-semibold">待办任务</p>
                                <h3 class="card-title font-bold">{{ todo_tasks }}</h3>
                            </div>
                            <i class="fa fa-hourglass-half fa-3x opacity-50"></i>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card bg-info text-white h-100">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <p class="card-text text-sm font-semibold">进行中任务</p>
                                <h3 class="card-title font-bold">{{ in_progress_tasks }}</h3>
                            </div>
                            <i class="fa fa-spinner fa-3x opacity-50"></i>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-md-3 mb-3">
                <div class="card bg-success text-white h-100">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center">
                            <div>
                                <p class="card-text text-sm font-semibold">已完成任务</p>
                                <h3 class="card-title font-bold">{{ completed_tasks }}</h3>
                            </div>
                            <i class="fa fa-check-circle fa-3x opacity-50"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 内容区域 - 两列布局 -->
        <div class="row">
            <!-- 左侧：最近任务和截止日期 -->
            <div class="col-lg-6 mb-4">
                <!-- 最近任务 -->
                <div class="card mb-4">
                    <div class="card-header bg-white border-bottom border-gray-200">
                        <h4 class="card-title mb-0">
                            <i class="fa fa-clock-o mr-2"></i>最近任务
                            <a href="{{ url_for('task_list') }}" class="btn btn-sm btn-primary float-end">查看全部</a>
                        </h4>
                    </div>
                    <div class="card-body">
                        {% if recent_tasks %}
                            <ul class="list-group list-group-flush">
                                {% for task in recent_tasks %}
                                    <li class="list-group-item d-flex justify-content-between align-items-center">
                                        <div class="flex-grow-1">
                                            <a href="{{ url_for('view_task', task_id=task.id) }}" class="text-decoration-none text-dark">
                                                <h6 class="mb-1">{{ task.title }}</h6>
                                            </a>
                                            <p class="text-sm text-muted mb-0">
                                                <span class="badge bg-{{ 'danger' if task.priority == 'high' else 'warning' if task.priority == 'medium' else 'success' }}"> {{ task.priority | capitalize }}</span>
                                                <span class="ml-2 badge bg-{{ 'secondary' if task.status == 'todo' else 'info' if task.status == 'in_progress' else 'success' if task.status == 'completed' else 'dark' }}"> {{ task.status | capitalize }}</span>
                                                {% if task.category %}
                                                <span class="ml-2 badge" style="background-color: {{ task.category.color }}">{{ task.category.name }}</span>
                                                {% endif %}
                                            </p>
                                        </div>
                                        <span class="text-xs text-muted ml-3">
                                            {{ task.created_at.strftime('%m-%d %H:%M') }}
                                        </span>
                                    </li>
                                {% endfor %}
                            </ul>
                        {% else %}
                            <div class="text-center text-muted py-4">
                                <i class="fa fa-inbox fa-3x mb-2"></i>
                                <p>暂无任务记录</p>
                                <a href="{{ url_for('add_task') }}" class="btn btn-sm btn-primary mt-2">创建第一个任务</a>
                            </div>
                        {% endif %}
                    </div>
                </div>

                <!-- 截止日期临近的任务 -->
                <div class="card">
                    <div class="card-header bg-white border-bottom border-gray-200">
                        <h4 class="card-title mb-0">
                            <i class="fa fa-calendar-check-o mr-2"></i>即将截止的任务
                        </h4>
                    </div>
                    <div class="card-body">
                        {% if upcoming_deadlines %}
                            <ul class="list-group list-group-flush">
                                {% for task in upcoming_deadlines %}
                                    <li class="list-group-item">
                                        <div class="d-flex justify-content-between">
                                            <div>
                                                <a href="{{ url_for('view_task', task_id=task.id) }}" class="text-decoration-none text-dark">
                                                    <h6 class="mb-1">{{ task.title }}</h6>
                                                </a>
                                                <p class="text-sm text-muted">
                                                    <i class="fa fa-calendar mr-1"></i>截止日期：{{ task.due_date.strftime('%Y-%m-%d %H:%M') }}
                                                </p>
                                            </div>
                                            <span class="badge bg-danger">
                                                {% set days_left = (task.due_date - datetime.utcnow()).days %}
                                                {% set hours_left = ((task.due_date - datetime.utcnow()).seconds // 3600) %}
                                                {{ days_left }}天{{ hours_left }}小时后
                                            </span>
                                        </div>
                                    </li>
                                {% endfor %}
                            </ul>
                        {% else %}
                            <div class="text-center text-muted py-4">
                                <i class="fa fa-calendar-o fa-3x mb-2"></i>
                                <p>没有即将截止的任务</p>
                            </div>
                        {% endif %}
                    </div>
                </div>
            </div>

            <!-- 右侧：任务统计图表和快速操作 -->
            <div class="col-lg-6">
                <!-- 任务统计图表 -->
                <div class="card mb-4">
                    <div class="card-header bg-white border-bottom border-gray-200">
                        <h4 class="card-title mb-0">
                            <i class="fa fa-pie-chart mr-2"></i>任务分布
                        </h4>
                    </div>
                    <div class="card-body">
                        <div class="row">
                            <div class="col-md-6">
                                <div class="chart-container" style="position: relative; height:200px;">
                                    <canvas id="taskStatusChart"></canvas>
                                </div>
                                <div class="text-center mt-2">
                                    <h6 class="text-sm font-medium text-gray-500">按状态分布</h6>
                                </div>
                            </div>
                            <div class="col-md-6">
                                <div class="chart-container" style="position: relative; height:200px;">
                                    <canvas id="taskPriorityChart"></canvas>
                                </div>
                                <div class="text-center mt-2">
                                    <h6 class="text-sm font-medium text-gray-500">按优先级分布</h6>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 快速操作 -->
                <div class="card">
                    <div class="card-header bg-white border-bottom border-gray-200">
                        <h4 class="card-title mb-0">
                            <i class="fa fa-bolt mr-2"></i>快速操作
                        </h4>
                    </div>
                    <div class="card-body">
                        <div class="grid grid-cols-2 gap-4">
                            <a href="{{ url_for('add_task') }}" class="btn btn-primary btn-block p-4 text-center">
                                <i class="fa fa-plus-circle fa-3x mb-2"></i>
                                <div>创建新任务</div>
                            </a>
                            <a href="{{ url_for('add_category') }}" class="btn btn-secondary btn-block p-4 text-center">
                                <i class="fa fa-folder-open fa-3x mb-2"></i>
                                <div>添加分类</div>
                            </a>
                            <a href="{{ url_for('reports') }}" class="btn btn-info btn-block p-4 text-center">
                                <i class="fa fa-bar-chart fa-3x mb-2"></i>
                                <div>查看报表</div>
                            </a>
                            <a href="{{ url_for('settings') }}" class="btn btn-warning btn-block p-4 text-center">
                                <i class="fa fa-cog fa-3x mb-2"></i>
                                <div>个人设置</div>
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 引入Bootstrap JS -->
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
    <!-- 引入自定义JS -->
    <script src="{{ url_for('static', filename='js/main.js') }}"></script>

    <!-- 图表初始化 -->
    <script>
        // 初始化任务状态分布图表
        const statusCtx = document.getElementById('taskStatusChart').getContext('2d');
        const statusChart = new Chart(statusCtx, {
            type: 'doughnut',
            data: {
                labels: ['待办', '进行中', '已完成', '已归档'],
                datasets: [{
                    data: [
                        {{ todo_tasks }},
                        {{ in_progress_tasks }},
                        {{ completed_tasks }},
                        {{ total_tasks - todo_tasks - in_progress_tasks - completed_tasks }}
                    ],
                    backgroundColor: [
                        '#6c757d',  // 待办 - 灰色
                        '#17a2b8',  // 进行中 - 蓝色
                        '#28a745',  // 已完成 - 绿色
                        '#343a40'   // 已归档 - 深灰色
                    ],
                    borderWidth: 0
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                cutout: '70%',
                plugins: {
                    legend: {
                        position: 'bottom',
                        labels: {
                            boxWidth: 12,
                            padding: 10,
                            font: {
                                size: 11
                            }
                        }
                    }
                }
            }
        });

        // 初始化任务优先级分布图表
        const priorityCtx = document.getElementById('taskPriorityChart').getContext('2d');
        const priorityChart = new Chart(priorityCtx, {
            type: 'doughnut',
            data: {
                labels: ['高优先级', '中优先级', '低优先级'],
                datasets: [{
                    data: [
                        {{ Task.query.filter_by(user_id=user.id, priority='high').count() }},
                        {{ Task.query.filter_by(user_id=user.id, priority='medium').count() }},
                        {{ Task.query.filter_by(user_id=user.id, priority='low').count() }}
                    ],
                    backgroundColor: [
                        '#dc3545',  // 高优先级 - 红色
                        '#ffc107',  // 中优先级 - 黄色
                        '#28a745'   // 低优先级 - 绿色
                    ],
                    borderWidth: 0
                }]
            },
            options: {
                responsive: true,
                maintainAspectRatio: false,
                cutout: '70%',
                plugins: {
                    legend: {
                        position: 'bottom',
                        labels: {
                            boxWidth: 12,
                            padding: 10,
                            font: {
                                size: 11
                            }
                        }
                    }
                }
            }
        });
    </script>
</body>
</html>